import React, { useState } from 'react'
import { Switch } from 'antd'
import { SwitchProps } from 'antd/lib/switch'

export default function StateSwitchBtn(props: IStateSwitchBtnProps) {
  const { loading, checked: initChecked, onClick, onChange, clickHandle, ...reset } = props
  const [checked, setChecked] = useState(initChecked)
  const switchOnClick = (checked, event) => {
    clickHandle && clickHandle(checked, (status) => {
      setChecked(status)
    })
    onClick && onClick(checked, event)
  }
  return (
    <Switch
      checkedChildren="开"
      unCheckedChildren="关"
      loading={loading}
      checked={checked}
      onClick={switchOnClick}
      {...reset}
    />
  )
}

interface IStateSwitchBtnProps extends SwitchProps {
  clickHandle?: (checked: boolean, cb: (status) => void) => void
}
